package com.hongtech.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.service.impl.CrudServiceImpl;
import com.hongtech.modules.system.dto.SysRoleMenuDTO;
import com.hongtech.modules.system.entity.SysRoleMenuEntity;
import com.hongtech.modules.system.mapper.SysRoleMenuMapper;
import com.hongtech.modules.system.service.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * SysRoleMenu 表数据服务层接口实现类
 */
@Service
public class SysRoleMenuServiceImpl extends CrudServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity,
        SysRoleMenuDTO> implements SysRoleMenuService {

    @Override
    public void addAuth(Long roleId, String menuIds) {
        // 删除原权限
        LambdaQueryWrapper<SysRoleMenuEntity> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(SysRoleMenuEntity::getRoleId, roleId);
        this.remove(deleteWrapper);
        // 插入新权限
        if (StringUtils.isNotEmpty(menuIds)) {
            String[] menuIdArray = menuIds.split(",");
            for (String menuId : menuIdArray) {
                SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
                sysRoleMenuEntity.setRoleId(roleId);
                sysRoleMenuEntity.setMenuId(Long.valueOf(menuId));
                this.save(sysRoleMenuEntity);
            }
        }
    }

}
